from flask import Flask, render_template, request, redirect, url_for, jsonify
import pymysql

app = Flask(__name__)

# 数据库连接配置
def get_db_connection():
    conn = pymysql.connect(
        host='localhost',
        user='root',
        password='666666',
        db='EventSphere_host_full',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )
    return conn

# 首页，展示所有活动
@app.route('/')
def index():
    conn = get_db_connection()
    cursor = conn.cursor()
    cursor.execute("""
        SELECT events.*, categories.category_name, ticket_types.ticket_name, ticket_types.ticket_price
        FROM events
        JOIN categories ON events.category_id = categories.id
        JOIN ticket_types ON events.ticket_type_id = ticket_types.id
    """)
    events = cursor.fetchall()
    conn.close()
    return render_template('index.html', events=events)

# 添加事件
@app.route('/add_event', methods=['GET', 'POST'])
def add_event():
    if request.method == 'POST':
        event_name = request.form['event_name']
        event_date = request.form['event_date']
        location = request.form['location']
        latitude = request.form['latitude']
        longitude = request.form['longitude']
        category_id = request.form['category_id']
        ticket_type_id = request.form['ticket_type_id']

        conn = get_db_connection()
        cursor = conn.cursor()
        cursor.execute("""
            INSERT INTO events (event_name, event_date, location, latitude, longitude, category_id, ticket_type_id)
            VALUES (%s, %s, %s, %s, %s, %s, %s)
        """, (event_name, event_date, location, latitude, longitude, category_id, ticket_type_id))
        conn.commit()
        conn.close()
        return redirect(url_for('index'))

    conn = get_db_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM categories")
    categories = cursor.fetchall()
    cursor.execute("SELECT * FROM ticket_types")
    ticket_types = cursor.fetchall()
    conn.close()

    return render_template('add_event.html', categories=categories, ticket_types=ticket_types)

# 编辑事件
@app.route('/edit_event/<int:event_id>', methods=['GET', 'POST'])
def edit_event(event_id):
    conn = get_db_connection()
    cursor = conn.cursor()

    if request.method == 'POST':
        event_name = request.form['event_name']
        event_date = request.form['event_date']
        location = request.form['location']
        latitude = request.form['latitude']
        longitude = request.form['longitude']
        category_id = request.form['category_id']
        ticket_type_id = request.form['ticket_type_id']

        cursor.execute("""
            UPDATE events
            SET event_name = %s, event_date = %s, location = %s, latitude = %s, longitude = %s, category_id = %s, ticket_type_id = %s
            WHERE id = %s
        """, (event_name, event_date, location, latitude, longitude, category_id, ticket_type_id, event_id))
        conn.commit()
        conn.close()
        return redirect(url_for('index'))

    cursor.execute("SELECT * FROM events WHERE id = %s", (event_id,))
    event = cursor.fetchone()
    cursor.execute("SELECT * FROM categories")
    categories = cursor.fetchall()
    cursor.execute("SELECT * FROM ticket_types")
    ticket_types = cursor.fetchall()
    conn.close()

    return render_template('edit_event.html', event=event, categories=categories, ticket_types=ticket_types)

# 删除事件 (使用AJAX删除)
@app.route('/delete_event/<int:event_id>', methods=['POST'])
def delete_event(event_id):
    conn = get_db_connection()
    cursor = conn.cursor()
    cursor.execute("DELETE FROM events WHERE id = %s", (event_id,))
    conn.commit()
    conn.close()
    return jsonify({'status': 'success', 'message': 'Event deleted successfully'})

if __name__ == '__main__':
    app.run(debug=True)
